package com.chiffoyqc.utils;

/**
 * Created by Administrator on 2017/7/4.
 * 用于16进制字符串与byte的转换
 */
public class HexUtils {

    /**
     * 16进制表示的字符串转换为字节数组
     *
     * @param s 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static short[] hexStringToShortArray(String s) {
        int len = s.length();
        short[] b = new short[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            b[i / 2] = (short) ((Character.digit(s.charAt(i), 16) << 4) + Character
                    .digit(s.charAt(i + 1), 16));
        }
        return b;
    }


    /**
     * 将字符串转化为ascii码中的16进制
     */
    public static short[] convertStringToHex(String str) {

        char[] chars = str.toCharArray();
        short[] shorts = new short[str.length()];

        for (int i = 0; i < chars.length; i++) {
            shorts[i] = (short) chars[i];
        }

        return shorts;
    }

    /**
     * 将ascii码中的16进制转化为对应的字符串
     */
    public static String convertHexToString(String hex) {

        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();

        //49204c6f7665204a617661 split into two characters 49, 20, 4c...
        for (int i = 0; i < hex.length() - 1; i += 2) {

            //grab the hex in pairs
            String output = hex.substring(i, (i + 2));
            //convert hex to decimal
            int decimal = Integer.parseInt(output, 16);
            //convert the decimal to character
            sb.append((char) decimal);

            temp.append(decimal);
        }

        return sb.toString();
    }

    public static short[] hexString2ToBytes2(String hexStr) {
        short[] b1 = new short[1];
        short[] b2 = new short[1];

        char[] chars = hexStr.toCharArray();
        short[] result = new short[hexStr.length() / 2];

        for (int i = 0; i < (hexStr.length() / 2); i++) {
//            b1 = ACCE1.GetBytes(hexStr[2 * i].ToString());
            b1[0] = (short) (chars[2 * i] - 0x41);
//            b1[0] = (byte)(b1[0] - 0x41);
//            b2 = ACCE1.GetBytes(hexStr[2 * i + 1].ToString());
            b2[0] = (short) (chars[2 * i + 1] - 0x41);
//            b2[0] = (byte) (b2[0] - 0x41);

            result[i] = (short) ((b1[0] << 4) + b2[0]);
        }

        return result;

    }

    /**
     * gprs模块的加密算法
     */
    public static String shortsTo2HexString(short[] b) {
        StringBuffer ret = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            ret.append((char) ((b[i] >> 4) + 0x41));
            ret.append((char) ((b[i] & 0x0F) + 0x41));
        }

        return ret.toString();

    }

    /**
     * gprs工业表 将收到的数据转为可见字符
     */
    public static int[] bytesTo2Char(int[] bs, int clen) {
        int[] ints = new int[clen];

        for (int j = 0; j < clen; j++) {

            if (j % 2 == 0x00) {
                if ((bs[j / 2] >> 4) > 0x09)
                    ints[j] = ((bs[j / 2] >> 4) - 0x09 + 0x40);
                else
                    ints[j] = ((bs[j / 2] >> 4) + 0x30);
            } else {
                if ((bs[j / 2] % 16) > 0x09)
                    ints[j] = ((bs[j / 2] % 16) - 0x09 + 0x40);
                else
                    ints[j] = ((bs[j / 2] % 16) + 0x30);
            }
        }
        return ints;

    }

}
